<template>
<!--  故障-告警处理率-->
  <div>
    <div id="gzcll"></div>
  </div>
</template>

<script>
import moment from 'moment'
const echarts = require('echarts/lib/echarts')
export default {
  name: 'gzcll',
  props: ['chart'],
  data() {
    return {
      myChart: null,
      barWidth: 6
    }
  },
  destroyed() {
    window.removeEventListener('resize', this.resizeFn, true);
    this.myChart?.dispose();
  },
  methods: {
    getChartData(network) {
      this.$http.get('/dataView/getAlarmAndFaultData', {
        params: { network }
      }).then(res => {
        this.initChart(res)
      }).catch(err => {
        this.$TZmessage.error(err.msg || '获取数据失败')
      })
    },
    initChart(val) {
      let chartDom = document.getElementById('gzcll');
      if (!chartDom) {return}
      this.myChart = echarts.init(chartDom);
      const list = val.map(item => {
        const { dataSize, count } = item
        return {
          ...item,
          date: moment(item.date).add(-1, 'days').format('MM/DD'),
          rate: (Math.round(dataSize / count * 10000) / 100).toFixed(2)
        }})
      const guzhang = list.filter(item => item.name === '故障')
      const gaojing = list.filter(item => item.name === '告警')

      let option = {
        tooltip: {
          show: true
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        legend: {
          data: ['故障', '告警'],
          top: '5%',
          right: '4%',
          textStyle: {
            color: '#fff',
            fontSize: 14 // 可选，增加字体大小以增强可读性
          }
        },
        xAxis: {
          type: 'category',
          data: guzhang.map(item => item.date),
          axisLabel: {
            textStyle: {
              color: '#ffffff'
            }
          }
        },
        yAxis: {
          type: 'value',
          axisLabel: {
            formatter: '{value} %', // 在 Y 轴标签上添加单位
            textStyle: {
              color: '#ffffff'
            }
          },
          axisTick: {
            show: false // 根据需要调整
          },
          splitLine: {
            show: true, // 根据需要调整
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.2)' // 浅灰色网格线，增强可读性
            }
          }
        },
        series: [
          {
            name: '故障',
            type: 'line',
            color: 'rgba(72,224,214)',
            data: guzhang.map(item => item.rate),
            showSymbol: true,
            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: 'rgba(72,224,214,0.2)' },
                { offset: 1, color: 'rgba(72,224,214,0.1)' }
              ])
            }
          },
          {
            name: '告警',
            type: 'line',
            color: '#4992FF',
            data: gaojing.map(item => item.rate),
            showSymbol: true,
            symbolSize: 6,
            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: 'rgba(72,224,214,0.2)' },
                { offset: 1, color: 'rgba(72,224,214,0.1)' }
              ])
            }
          }
        ]
      };
      this.myChart.setOption(option);

      option && this.myChart.setOption(option);

      window.addEventListener('resize', this.resizeFn, true);
    },
    resizeFn() {
      this.myChart.resize();
    }
  }
}
</script>

<style lang="scss" scoped>
#gzcll {
  width: 100%;
  height: 218px;
}
</style>
